//
// Created by Jisam on 2024/6/29.
//
#include <bits/stdc++.h>
#define i64 long long
using namespace  std;

struct node{
    int s,b;
};

int ss = 1 ,bb = 0;
const int N = 10;
int vis[N];
node a[N];
int ans = INT_MAX;
void dfs(int n,int i){
    if(i == n){
        return ;
    }
    for(int i = 0 ; i < n ; i ++){
        if(vis[i]) continue;
        vis[i] = true;
        ss *= a[i].s;
        bb += a[i].b;
        ans = min(ans,abs(ss - bb));
        dfs(n, i + 1);
        ss /= a[i].s;
        bb -= a[i].b;
        vis[i] = false;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for(int i = 0 ; i < n ;i ++) cin >> a[i].s >> a[i].b;

    dfs(n,0);

    cout << ans;
    return  0;
}